//
//  YYToast.swift
//  XinMi_Swift
//
//  Created by 蓓蕾 on 2022/3/12.
//

import UIKit

class YYToast: NSObject {
    
    
    //屏高
    let kScreenHeight = UIScreen.main.bounds.size.height
    /** 屏宽 */
    let kScreenWidth = UIScreen.main.bounds.size.width
    
    var bgView : UIView?
    
    var titleLabel: UILabel?
    
    private let statusFont = UIFont.boldSystemFont(ofSize: 16.0)
    private var statusTextColor: UIColor!
    private var statusBgColor: UIColor!
    
    static let shared = YYToast()
    
    private override init() {
        statusTextColor = UIColor.white
        statusBgColor = UIColor.black
        print("init 方法")
    }
    
    override func copy() -> Any {
        print("copy 方法")
        return self // SingletonClass.shared
    }
    
    override func mutableCopy() -> Any {
        print("mutableCopy 方法")
        return self // SingletonClass.shared
    }
    
    func reset() {
        print("reset 方法")
    }
    /// @brief 在中心显示一个信息提示
    func toastMessage(status: String){
        initUI()
        titleLabel?.text = status
        addjustSize()
        bgView?.isHidden = false
        startDelayedExecutionMethod()
    }
    /// @brief 在底部显示一个信息提示
    func toastBottomMessage(status: String){
        initUI()
        titleLabel?.text = status
        addjustSize()
        let width:CGFloat = (bgView?.bounds.size.width)!
        let height:CGFloat = (bgView?.bounds.size.height)!
        bgView?.frame = CGRect(x: (kScreenWidth - width) / 2, y: kScreenHeight - height - 40, width: width, height: height)
        bgView?.isHidden = false
        startDelayedExecutionMethod()
    }
    /// @brief 在底部显示一个信息提示
    func toastBottomMarginMessage(status: String, margin: CGFloat){
        initUI()
        titleLabel?.text = status
        addjustSize()
        let width:CGFloat = (bgView?.bounds.size.width)!
        let height:CGFloat = (bgView?.bounds.size.height)!
        bgView?.frame = CGRect(x: (kScreenWidth - width) / 2, y: kScreenHeight - height - margin, width: width, height: height)
        bgView?.isHidden = false
        startDelayedExecutionMethod()
    }
    /// @brief 在指定位置显示一个信息提示
    func toastPointMessage(status: String, point: CGPoint){
        initUI()
        titleLabel?.text = status
        addjustSize()
//        let bgViewBounds:CGRect = bgView?.bounds
        bgView?.center = point
        bgView?.isHidden = false
        startDelayedExecutionMethod()
    }
    
    private func startDelayedExecutionMethod(){
        NSObject.cancelPreviousPerformRequests(withTarget: self)
        self.perform(#selector(delayedExecutionMethod), with: nil, afterDelay: 3)
    }
    
    @objc func delayedExecutionMethod(){
        bgView?.isHidden = true
    }
    
    private func initUI(){
        if bgView == nil {
            bgView = UIView(frame: CGRect.zero)
            bgView?.backgroundColor = statusBgColor
            bgView?.layer.cornerRadius = 10;
            bgView?.layer.masksToBounds = true
        }
        if bgView!.superview == nil {
            getWindow().addSubview(bgView!)
        }
        
        if titleLabel == nil {
//            statusTextColor = UIColor.black
            titleLabel = UILabel(frame: CGRect.zero)
            titleLabel?.backgroundColor = UIColor.clear
            titleLabel?.font = statusFont
            titleLabel?.textColor = statusTextColor
            titleLabel?.baselineAdjustment = UIBaselineAdjustment.alignCenters
            titleLabel?.numberOfLines = 0
            titleLabel?.numberOfLines = 0
            titleLabel?.textAlignment = NSTextAlignment.center
            titleLabel?.adjustsFontSizeToFitWidth = false
        }
        if titleLabel!.superview == nil {
            bgView?.addSubview(titleLabel!)
        }
    }
    
    /// @brief 调整大小
    private func addjustSize(){
        var rect = CGRect.zero
        var width: CGFloat = 100.0
        var height: CGFloat = 100.0
        
//        var textWidth:CGFloat = 0
//        var textHeight:CGFloat = 0
        
        /// 计算文本大小
        if titleLabel!.text != nil {
            let style = NSMutableParagraphStyle()
            style.lineBreakMode = NSLineBreakMode.byCharWrapping
            let attributes = [NSAttributedString.Key.font: statusFont, NSAttributedString.Key.paragraphStyle: style.copy()]
            let option = NSStringDrawingOptions.usesLineFragmentOrigin
            let text: NSString = NSString(cString: titleLabel!.text!.cString(using: String.Encoding.utf8)!,
                                          encoding: String.Encoding.utf8.rawValue)!
            rect = text.boundingRect(with:CGSize(width: kScreenWidth - 100, height: kScreenHeight - 200), options: option, attributes: attributes, context: nil)
            rect.origin.x = 12
            rect.origin.y = 66
            
//            let textWidth = rect.size.width
//            let textHeight = rect.size.height
            
            
            width = rect.size.width
            height = rect.size.height
            
//            if width < 100 {
//                width = 100
//                rect.origin.x = 0
//                rect.size.width = 100
//            }
        }
        
//        titleLabel!.center = CGPoint(x: kScreenWidth / 2, y: kScreenHeight / 2)
//        titleLabel!.bounds = CGRect(x: 0, y: 0, width: width, height: height)
        
        
        bgView?.center = CGPoint(x: kScreenWidth / 2, y: kScreenHeight / 2)
        bgView!.bounds = CGRect(x: 0, y: 0, width: (width + 20), height: (height + 30))
        
//        let h = titleLabel!.text == nil ? height / 2 : 36
//        imageView!.center = CGPoint(x: width / 2, y: h)
//        spinner!.center = CGPoint(x: width / 2, y: h)
//
        titleLabel!.frame = CGRect(x: 10, y: 15, width: width, height: height)
    }
    
    /// @brief 获取窗口window
    private func getWindow() ->UIWindow {
        //            if let delegate: UIApplicationDelegate = UIApplication.shared.delegate{//UIApplication.sharedApplication().delegate {
        //                if let window = delegate.window {
        //                    return window!
        //                }
        //            }
        
        return UIApplication.shared.keyWindow!
    }
    
    
    
    
    

}

